import 'package:flutter/material.dart';
import 'package:xiongan_app_yy/common/user_singleton.dart';
import 'package:xiongan_app_yy/entrys/rp/address_book_info_entity.dart';
import 'package:xiongan_app_yy/net/internet_api.dart';
import 'package:xiongan_app_yy/pages/common/LoadBuilder.dart';
import 'package:xiongan_app_yy/widgets/comm/left_icon_widget.dart';

class AddressBookPage extends StatefulWidget {
  @override
  _AddressBookPageState createState() => _AddressBookPageState();
}

class _AddressBookPageState extends State<AddressBookPage> {
  List<AddressBookInfoData> datas = [];
  bool show = true;

  @override
  void initState() {
    super.initState();
    getdata();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          leading: LeftIconWidget(icon: Icons.chevron_left, isize: 36),
          backgroundColor: Color(0XFF00A5F1),
          title: Text(
            "通讯录",
            style: TextStyle(fontSize: 18),
          ),
          actions: <Widget>[
            new IconButton(
              // action button
              padding: EdgeInsets.only(),
              iconSize: 22,
              icon: new Icon(IconData(0xe617, fontFamily: "wuIconFont")),
              onPressed: () {
                Navigator.pushNamed(context, "/addressBookSearch");
              },
            )
          ],
          elevation: 0,
          centerTitle: true,
        ),
        body: LoadBuilder(
          visibility: show,
          builder: ListView.builder(
            itemBuilder: (context, index) {
              return Container(
                color: Colors.white,
                child: ExpansionTile(
                    backgroundColor: Colors.white70,
                    title: Text("${datas[index].departmentname}"),
                    children: datas[index]
                        .userList
                        .map((info) => ListTile(
                              title: Text(info.username),
                              subtitle: Text(info.useraccount),
                              onTap: () {
                                Navigator.pushNamed(
                                    context, "/personal_data_page", arguments: {
                                  "id": info.userid,
                                  "other": true
                                });
                              },
                            ))
                        .toList()),
              );
            },
            itemCount: datas.length,
          ),
        ));
  }

  void getdata() {
    InternetAPI.getInstance()
        .get("WConfiguration/PptUser/GetDepartmentUserSelectItemList", (data) {
      setState(() {
        show = false;
        datas = AddressBookInfoEntity.fromJson(data).datas;
      });
    }, params: {
      'operaterUserId': DataSingleton().Data.id,
      'tenantId': DataSingleton().Data.tenantId,
    });
  }
}
